import os
import sys
import random
import json

if __name__ == "__main__":
    # 在默认的环境中运行（第一个参数是Django运行的配置文件，第二个参数是当前项目运行的配置文件）
    pwd = os.path.dirname(os.path.realpath(__file__))
    # sys.path.append(pwd + "../")
    os.environ.setdefault("DJANGO_SETTINGS_MODULE", "izone.settings")


    # 导入Django
    import django

    # 运行Django项目
    django.setup()

    # 对数据库中的表的相关操作
    from logAdmin import models as logAdminModels
    import datetime

    # article_list = models.Article.objects.all().values('id', 'title', "sourse__name")
    # # for i in article_list:
    # #     i.create_time = datetime.datetime.now()
    # #     i.update_time = datetime.datetime.now()
    # #     i.save()
    # print(article_list)
    # print(logAdminModels.LogInfo.objects.all())

    from utils.TestLogDate import split_log

    # data_list = split_log.get_nginx_log_data_01(file_path="utils/TestLogDate/nginx/access_sum_cp.log")
    #
    # # for item in data_list:
    # #     print(item)
    #
    # num = 0
    # for item in data_list:
    #     try:
    #         logAdminModels.LogInfo.objects.create(**item)
    #         num += 1
    #         print(num, 'ok')
    #     except Exception as e:
    #         print(e)
    #         num += 1
    #         print('第{}行插入失败'.format(num))
    #         continue
    # print('finish')
    #
    # items = logAdminModels.LogInfo.objects.all()
    # print(len(items))

    # num = 0
    # result = logAdminModels.LogInfo.objects.all()
    # for one_object in result:
    #     one_object.delete()
    #     num += 1
    #     print(num)

    # fp01 = open('utils/TestLogDate/source_url_list.txt', 'a+', encoding='utf-8')
    # fp01 = open('utils/TestLogDate/refer_list.txt', 'a+', encoding='utf-8')
    # fp01 = open('utils/TestLogDate/body_size_list.txt', 'a+', encoding='utf-8')
    #
    # items = logAdminModels.LogInfo.objects.all()
    # num = 0
    # for item in items:
    #     if item.status in range(200, 350):
    #         print(item.body_size)
    #         fp01.write(str(item.body_size))
    #         fp01.write('\n')
    #         num += 1
    #
    # print(len(items))
    # print(num)
    # fp01.close()
    log_list = list()
    log_objs = logAdminModels.LogInfo.objects.all()
    for one_log_obj in log_objs:

        one_log_dict = dict()
        one_log_dict["@timestamp"] = one_log_obj.visit_time  # $time_iso8601
        one_log_dict["@version"] = "1"
        one_log_dict["client"] = one_log_obj.ip
        one_log_dict["url"] = one_log_obj.source_url
        one_log_dict["status"] = one_log_obj.status
        one_log_dict["domain"] = 1
        one_log_dict["host"] = '172.19.170.14'
        one_log_dict["size"] = one_log_obj.body_size
        one_log_dict["responsentime"] = format(random.uniform(0.02, 17.04), '.3f')
        one_log_dict["useragent"] = one_log_obj.user_agent

        if one_log_dict["status"] > 390:
            one_log_dict["upstreampstatus"] = '_'
        else:
            one_log_dict["upstreampstatus"] = one_log_dict["status"]

        one_log_dict["upstreamaddr"] = '0.0.0.0:8031'
        one_log_dict["upstreamresponsetime"] = format(random.uniform(0.02, 0.07), '.3f')



        log_list.append(one_log_dict)

    # print(log_list)
    # print(len(log_list))
    json_log = json.dumps(log_list)
    print(json_log)

    with open('./nginx_log_sum.json', 'a+', encoding='utf-8') as fp:
        fp.write(json_log)

    # 2020-03-14T19:06:35+08:00  json日志格式
    # 30/Dec/2019:09:09:10 +0800 原来日志格式
    '''
    ip = models.CharField(max_length=30, default='171.88.100.129', verbose_name='访问ip')
    visit_time = models.CharField(max_length=100, verbose_name='访问时间', default='06/Jan/2020:11:55:4')
    request_method = models.CharField(max_length=30, default='GET', verbose_name='请求方法')
    source_url = models.URLField(max_length=300, verbose_name='访问的资源url', default='/')
    protocol = models.CharField(max_length=30, verbose_name='协议', default='HTTP/1.1')
    status = models.IntegerField(default=200, verbose_name='访问状态码')

    body_size = models.IntegerField(verbose_name='请求包字节大小', default=0)
    http_refer = models.URLField(default='/', verbose_name='http代理（从站点内哪访问的）')
    user_agent = models.TextField(default='-', verbose_name='UserAgent')
    '''